package FrameWork.Model;

import FrameWork.EventBus.AsyncResult;
import FrameWork.EventBus.Emitter;
import FrameWork.EventBus.EventBusManager;
import FrameWork.EventBus.ITask;

public class Player extends GFighter implements Emitter {

    private String account;
    private int gold;

    public Player(String account){
        this.account = account;
    }

    public void consumeGold(int amount, ITask<Boolean> cb){
        EventBusManager.submitTask(getDispatcherCode(),()->{
            AsyncResult<Boolean> as = new AsyncResult<>();
            as.setSuccess(true);
            cb.setValue(as);
            if (this.gold<amount){
                as.setValue(Boolean.FALSE);
            }else{
                this.alterGold(amount);
                as.setValue(Boolean.TRUE);
            }
            EventBusManager.submitTask(cb);
        });
    }



    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    @Override
    public int getGold() {
        return gold;
    }

    @Override
    public void setGold(int gold) {
        this.gold = gold;
    }

    public void alterGold(int gold){
        this.gold += gold;
    }

    @Override
    public int getDispatcherCode() {
        return this.account.hashCode();
    }

}
